/*
 * fileName: TimingTasksAction.java
 * author: ewangshi
 * createDate: 2013-8-7
 */
package com.ws.wsapp.chrome.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ws.app.common.ControllerSupport;
import com.ws.base.db.common.order.Order;
import com.ws.base.db.common.page.Page;
import com.ws.wsapp.chrome.entity.TimingTasks;
import com.ws.wsapp.chrome.service.TimingTasksService;

/**
 * @author ewangshi
 */
@Controller
public class TimingTasksAction extends ControllerSupport {

    // private WSAppLogger logger =
    // WSAppLogger.getWSAppLogger(TimingTasksAction.class);

    @Resource(name = "ws_chrome_timingTasksService")
    private TimingTasksService timingTasksService;

    @RequestMapping("/addTimingTasks")
    public void addTimingTasks(HttpServletRequest request,
            HttpServletResponse response) {
        String storeName = request.getParameter("storeName");
        String text = null;
        String date = null;

        if ("timingTasks".equals(storeName)) {
            date = request.getParameter("tDate");
            text = request.getParameter("tText");
        } else if ("notepad".equals(storeName)) {
            date = request.getParameter("date");
            text = request.getParameter("text");
        }

        TimingTasks timingTasks = new TimingTasks(storeName, text, date, 2);
        timingTasksService.addTimingTasks(timingTasks);
        this.outputText("success", response);
    }

    @RequestMapping("/queryTimingTasks")
    public void queryTimingTasks(HttpServletRequest request,
            HttpServletResponse response) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", 0);
        map.put("str", "AND ((DATE <= DATE_ADD(NOW(),INTERVAL 30 MINUTE)"
                + " AND STATUS = 2) OR (DATE <= DATE_ADD(NOW(),INTERVAL 5 "
                + "MINUTE) AND STATUS = 1))");
        Order order = new Order(true, "date");
        List<TimingTasks> list = timingTasksService.queryTimingTasks(map, null,
                order);

        this.outputJSON(list, response);
    }

    @RequestMapping("/queryTimingTasksTop10")
    public void queryTimingTasksTop10(HttpServletRequest request,
            HttpServletResponse response) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", 0);
        map.put("str", "AND ((DATE <= DATE_ADD(NOW(),INTERVAL 30 MINUTE)"
                + " AND STATUS = 2) OR (DATE <= DATE_ADD(NOW(),INTERVAL 5 "
                + "MINUTE) AND STATUS = 1))");
        Order order = new Order(true, "date");
        List<TimingTasks> list = timingTasksService.queryTimingTasks(map,
                new Page(), order);

        this.outputJSON(list, response);
    }

    @RequestMapping("/closeTimingTasks")
    public void closeTimingTasks(HttpServletRequest request,
            HttpServletResponse response) {
        String id = request.getParameter("id");
        String cmd = request.getParameter("cmd");
        TimingTasks tasks = null;
        if (null != cmd && "del".equals(cmd)) {
            tasks = new TimingTasks();
            tasks.setId(Integer.parseInt(id));
            tasks.setStatus(0);
        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", id);
            List<TimingTasks> timingTasks = timingTasksService
                    .queryTimingTasks(map, null, null);

            if (null != timingTasks && timingTasks.size() > 0) {
                tasks = timingTasks.get(0);
            }

            tasks.setStatus(tasks.getStatus() - 1);
        }

        //timingTasksService.updateTimingTasks(tasks);

        this.outputText("success", response);
    }
}
